package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.OrderInfo;
import com.itheima.entity.Result;
import com.itheima.service.OrderService;
import com.itheima.utils.YunSmsUtils;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Reference(version = "1.0.0")
    private OrderService orderService;
    @Autowired
    private JedisPool jedisPool;
   /* @Autowired
    private YunSmsUtils yunSmsUtils;*/

   @PostMapping("check")
   public Result check(@RequestBody OrderInfo orderInfo){
       String telephone = orderInfo.getTelephone();
       String validateCode = orderInfo.getValidateCode();
       Jedis jedis = null;
       try {
           jedis = jedisPool.getResource();
           String codeStr = jedis.get("ORDER" + telephone);
           if (codeStr == null) {
               return Result.fail(-999, "验证码已失效");
           } else {
               if (!codeStr.equals(validateCode)) {
                   return Result.fail(-999, "验证码不正确");
               }
           }
       } catch (Exception e) {
           e.printStackTrace();
           return Result.fail(-999, "系统异常，预约失败");
       } finally {
           if (jedis != null) {
               jedis.close();
           }
       }
       return Result.success(null);
   }

    @GetMapping("sms")
    public Result sms(String telephone) {
        int code = RandomUtils.nextInt(100000, 999999);
        System.out.println(code);
        /*   boolean flag = yunSmsUtils.sendYunzhixunMsg(telephone, code, 5 * 60);*/
        /*  if (flag) {*/
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.setex("ORDER" + telephone, 5 * 60, String.valueOf(code));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        /*  }*/

        return Result.success(true);
    }

    @PostMapping("save")
    public Result save(@RequestBody OrderInfo orderInfo) {
        String telephone = orderInfo.getTelephone();
        String validateCode = orderInfo.getValidateCode();
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String codeStr = jedis.get("ORDER" + telephone);
            if (codeStr == null) {
                return Result.fail(-999, "验证码已失效");
            } else {
                if (!codeStr.equals(validateCode)) {
                    return Result.fail(-999, "验证码不正确");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(-999, "系统异常，预约失败");
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }

        return orderService.save(orderInfo);
    }


    @PostMapping("/findById")
    public Result findById(Integer id) {
        return orderService.findById(id);
    }


}
